package com.itheima.mobilesafe74.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;
import com.itheima.mobilesafe74.db.dao.BlackNumberDao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/5/31.
 */
public class BlackNumberService extends Service {
    private InnerSmsReceiver mInnerSmsReceiver;
    private BlackNumberDao mDao;
    private TelephonyManager mTM;
    private MyPhoneStateListener mPhoneStateListener;
    private MyContentObserver mContentObserver;
    @Override
    public void onCreate() {
        mDao = BlackNumberDao.getInstance(getApplicationContext());
        //拦截短信
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.setPriority(1000);

        mInnerSmsReceiver = new InnerSmsReceiver();
        registerReceiver(mInnerSmsReceiver, intentFilter);

        //监听电话的状态
        //电话状态的监听（服务开启的时候需要去做监听，关闭的时候电话状态就不需要监听）
        //1、电话管理者对象
        mTM= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //2、监听电话状态
        mPhoneStateListener=new MyPhoneStateListener();
        mTM.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        super.onCreate();
    }
    class MyPhoneStateListener extends PhoneStateListener{
        //手动重写电话状态发生改变会触发的方法
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state){
                case TelephonyManager.CALL_STATE_IDLE:
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    //挂断电话
                    //mTM.endCall();
                    endCall(incomingNumber);
                    break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        //注销广播
        if (mInnerSmsReceiver != null) {
            unregisterReceiver(mInnerSmsReceiver);
        }
        //注销内容观察者
        if (mContentObserver!=null){
            getContentResolver().unregisterContentObserver(mContentObserver);
        }
        //取消对电话状态的监听
        if (mPhoneStateListener!=null){
            mTM.listen(mPhoneStateListener,PhoneStateListener.LISTEN_NONE);
        }
        super.onDestroy();
    }

    private class InnerSmsReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //获取短信内容，获取发送短信电话号码，如果此电话号码在黑名单上，并且拦截模式也为1（短信）或者3（所有）
            //1、获取短信内容
            Object[] objects = (Object[]) intent.getExtras().get("pdus");
            //2、循环遍历短信
            for (Object object : objects) {
                //3、获取短信对象
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) object);
                //4、获取短信对象的基本信息
                String originatingAddress = sms.getOriginatingAddress();
                String messageBody = sms.getMessageBody();


                int mode=mDao.getMode(originatingAddress);

                if (mode==1 || mode==3){
                    //拦截短信
                    abortBroadcast();
                }
            }
        }
    }

    public void endCall(String phone){

        int mode=mDao.getMode(phone);

        if (mode==2 || mode==3){
            //ITelephony.Stub.asInterface(ServiceManager.getService(getApplicationContext().TELEPHONY_SERVICE));
            //serviceManager此类a对ndroid开发者隐藏，所以不能去直接调用其方法，需要反射调用
            try {
                //1、获取ServiceManager字节码文件
                Class<?> clazz=Class.forName("android.os.ServiceManager");
                //2、获取方法
                Method method=clazz.getMethod("getService",String.class);
                //3、反射调用方法
                IBinder iBinder= (IBinder) method.invoke(null,Context.TELEPHONY_SERVICE);
                //4、调用获取aidl文件对象的方法
                ITelephony iTelephony=ITelephony.Stub.asInterface(iBinder);
                //5、调用在aidl中移仓的endCall方法
                iTelephony.endCall();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        //6、删除此被拦截电话号码的通信记录（权限）


        //6、在内容解析器上，去注册内容观察者，通过内容观察者，观察数据库的变化（uri决定哪张表哪张库）
        mContentObserver=new MyContentObserver(new Handler(),phone);
        getContentResolver().registerContentObserver(Uri.parse("content://call_log/calls"),true,mContentObserver);
    }
    class MyContentObserver extends ContentObserver{

        private String phone;
        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public MyContentObserver(Handler handler,String phone) {
            super(handler);
            this.phone=phone;
        }

        /**
         * @param selfChange
         * 数据库指定calls表发生改变的时候去调用方法
         */
        @Override
        public void onChange(boolean selfChange) {
            //插入一条数据后在进行删除
            getContentResolver().delete(Uri.parse("content://call_log/calls"),"number=?",new String[]{phone});
            super.onChange(selfChange);
        }
    }
}
